package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealdishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class dishServiceImpl implements DishService {


    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealdishMapper setmealdishMapper;

    /**
     * 增加菜品和对应的口味
     * @param dishDTO
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        //插入菜品后，获取主键id，是通过insert属性 useGeneratedKeys="true" keyProperty="id"实现的
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            for(DishFlavor df:flavors){
                df.setDishId(dishId);
            }
            dishFlavorMapper.insetBatch(flavors);
        }
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pagequery(DishPageQueryDTO dishPageQueryDTO) {

        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

        Page<DishVO> page=dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deletePatch(List<Long> ids) {

        for (Long id:ids){
           Dish dish = dishMapper.select(id);

            if(dish.getStatus().equals(StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(dish.getName()+MessageConstant.DISH_ON_SALE);
            }
        }

        List<SetmealDish> getsetargs=setmealdishMapper.getSetmealdishIdsByDishId(ids);
        if(getsetargs.size()>0){
            throw new DeletionNotAllowedException(getsetargs.get(0).getName()+MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }


/**
 * 条件通过，开始删除菜品和口味
 */
        dishMapper.delete(ids);
        dishFlavorMapper.deletBydishId(ids);
//    for(Long dishid:ids){
//        dishMapper.delete(dishid);
//
//        dishFlavorMapper.deletBydishId(dishid);
//    }

    }

    @Override
    public DishVO getByid(Long id) {

        DishDTO dishDTO=dishMapper.selecByid(id);
        List<DishFlavor> flavors=dishFlavorMapper.getByDishId(id);
        DishVO dishvo=new DishVO();
        BeanUtils.copyProperties(dishDTO,dishvo);
        dishvo.setFlavors(flavors);
        return dishvo;
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setUpdateUser(Thread.currentThread().getId());
        dishMapper.edit(dish);
        List<DishFlavor> flavores=dishDTO.getFlavors();
List<Long> dishidlist=new ArrayList<>();
dishidlist.add(dish.getId());

if(flavores.size()>0){
    dishFlavorMapper.deletBydishId(dishidlist);
    dishFlavorMapper.insetBatch(flavores);

}


    }

    @Override
    public void SetDishStatus(Integer status,Long id) {
        dishMapper.SetDishStatus(status,id);
    }



    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);
        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }
}
